package cn.com.duiba.millionaire.center.biz.service.impl;

import cn.com.duiba.credits.sdk.AssembleTool;
import cn.com.duiba.credits.sdk.CreditConsumeParams;
import cn.com.duiba.developer.center.api.domain.dto.AppSimpleDto;
import cn.com.duiba.developer.center.api.remoteservice.RemoteAppService;
import cn.com.duiba.millionaire.center.api.param.CreditsConsumeRequestParam;
import cn.com.duiba.millionaire.center.biz.mq.RocketMQTopicConstant;
import cn.com.duiba.millionaire.center.biz.service.CreditsConsumeService;
import cn.com.duiba.order.center.api.dto.CreditsMessage;
import cn.com.duiba.thirdparty.api.RemoteCreditsService;
import cn.com.duiba.thirdparty.dto.CreditsMessageDto;
import cn.com.duiba.thirdparty.enums.CallbackChannelTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaoxuda on 2018/2/23.
 */
@Service
public class CreditsConsumeServiceImpl implements CreditsConsumeService {
    @Autowired
    private RemoteAppService remoteAppService;
    @Autowired
    private RemoteCreditsService remoteCreditsService;
    @Autowired
    private RocketMQTopicConstant rocketMQTopicConstant;

    @Override
    public void apply(CreditsConsumeRequestParam rqParam, Long logId) {
        AppSimpleDto app = remoteAppService.getSimpleApp(rqParam.getAppId()).getResult();

        CreditsMessage creditsMessage = new CreditsMessage();
        creditsMessage.setAppId(rqParam.getAppId().toString());
        creditsMessage.setConsumerId(rqParam.getConsumerId().toString());
        creditsMessage.setHttpType(CreditsMessage.HTTP_GET);

        String orderNum = rqParam.getBizType() + "_" + logId.toString();
        String transfer = rqParam.getTransfer();
        CreditConsumeParams p = new CreditConsumeParams();
        p.setAppKey(app.getAppKey());
        p.setCredits(rqParam.getCredits().longValue());
        p.setOrderNum(orderNum);
        p.setUid(rqParam.getPartnerUserId());
        p.setTimestamp(new Date());
        p.setType(rqParam.getBizType().getType());
        p.setIp(rqParam.getEnvIp());
        p.setActualPrice(0);
        p.setDescription("游戏活动");
        if (StringUtils.isNotBlank(transfer)) {
            p.setTransfer(transfer);
        }
        Map<String, String> map = p.toRequestMap(app.getAppSecret());
        String url = AssembleTool.assembleUrl(app.getCreditsConsumeRequestUrl(), map);
        creditsMessage.setHttpUrl(url);
        creditsMessage.setRelationId(logId.toString());
        creditsMessage.setRelationType(rqParam.getBizType().getType());
        creditsMessage.setTimestamp(System.currentTimeMillis());
        Map<String, String> params = new HashMap<>();
        params.put("partnerUserId", rqParam.getPartnerUserId());
        params.put("orderNum", orderNum);
        params.put("credits", rqParam.getCredits().toString());
        params.put("logId", logId.toString());
        if (StringUtils.isNotBlank(transfer)) {
            params.put("transfer", transfer);
        }
        params.put(CreditsMessageDto.MESSAGE_CHANNEL_TYPE_KEY, CallbackChannelTypeEnum.ROCKETMQ.getType());
        creditsMessage.setParams(params);

        // 必须在组装url之后 AssembleTool.assembleUrl
        map.put("appSecret", app.getAppSecret());
        creditsMessage.setAuthParams(map);

        remoteCreditsService.submitSubCredits(creditsMessage, rocketMQTopicConstant.getCreditsConsumeTopic(),
                rocketMQTopicConstant.getCreditsConsumeMillTag(), null);
    }
}
